home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / DEMON / RISCOS2 / TCP_131S.ARC / c / AX25USER < prev    next >
Text File  |  1992-03-07  |  5KB  |  173 lines

  1. /* User subroutines for AX.25 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7. #include <stdarg.h>
  8. #include "global.h"
  9. #include "mbuf.h"
  10. #include "timer.h"
  11. #include "iface.h"
  12. #include "ax25.h"
  13. #include "lapb.h"
  14.  
  15. /* Open an AX.25 connection */
  16. struct ax25_cb *open_ax25(struct ax25 *addr, int16 window,
  17.                           void (*r_upcall)(), void (*t_upcall)(),
  18.                           void (*s_upcall)(),
  19.                           struct interface *iface, char *user)
  20. {
  21.         struct ax25_cb *axp;
  22.  
  23.         if((axp = cr_ax25(&addr->dest)) == NULLAX25)
  24.                 return NULLAX25;
  25.         axp->addr = *addr;
  26.         if(addr->ndigis != 0){
  27.                 axp->t1.start *= (addr->ndigis + 1);
  28.                 axp->t2.start *= (addr->ndigis + 1);
  29.                 axp->t3.start *= (addr->ndigis + 1);
  30.         }
  31.         axp->window = window;
  32.         axp->r_upcall = r_upcall;
  33.         axp->t_upcall = t_upcall;
  34.         axp->s_upcall = s_upcall;
  35.         axp->interface = iface;
  36.         axp->user = user;
  37.  
  38.         switch(axp->state){
  39.         case DISCONNECTED:
  40.                 /* Don't send anything if the connection already exists */
  41.                 est_link(axp);
  42.                 lapbstate(axp,SETUP);
  43.                 break;
  44.         case SETUP:
  45.                 free_q(&axp->txq);
  46.                 break;
  47.         case DISCPENDING:       /* Ignore */
  48.         case FRAMEREJECT:
  49.                 break;
  50.         case RECOVERY:
  51.         case CONNECTED:
  52.                 free_q(&axp->txq);
  53.                 est_link(axp);
  54.                 lapbstate(axp,SETUP);
  55.                 break;
  56.         }
  57.         return axp;
  58. }
  59.  
  60. /* Send data on an AX.25 connection. Caller must provide PID */
  61. int send_ax25(struct ax25_cb *axp, struct mbuf *bp)
  62. {
  63.         if(axp == NULLAX25 || bp == NULLBUF)
  64.                 return -1;
  65.         enqueue(&axp->txq,bp);
  66.         return lapb_output(axp);
  67. }
  68.  
  69. /* Do printf on a ax25 connection */
  70. int aprintf(struct ax25_cb *axp, char *message, ...)
  71. {
  72.         va_list argptr;
  73.         struct mbuf *bp;
  74.         int len;
  75.         register char *cp;
  76.  
  77.         if(axp == NULLAX25)
  78.                 return 0;
  79.  
  80.         if((bp = alloc_mbuf(axp->paclen+1)) != NULLBUF)
  81.         {
  82.                 va_start(argptr,message);
  83.                 cp = bp->data;
  84.                 *cp++ = PID_NO_L3;               
  85.                 len = vsprintf(cp,message,argptr);
  86.                 va_end(argptr);
  87.                 cp = bp->data;
  88.                 cp++;
  89.                 bp->cnt = (strlen(cp) + 1);
  90.                 send_ax25(axp,bp);
  91.                 return len;
  92.         }
  93.         else    
  94.         {
  95.                 return 0;
  96.         }
  97. }
  98.  
  99. /* Receive incoming data on an AX.25 connection */
  100. struct mbuf *recv_ax25(register struct ax25_cb *axp, int16 cnt)
  101. {
  102.         struct mbuf *bp;
  103.  
  104.         cnt = cnt;
  105.  
  106.         if(axp->rxq == NULLBUF)
  107.                 return NULLBUF;
  108.  
  109.         bp = axp->rxq;
  110.         axp->rxq = NULLBUF;
  111.  
  112.         /* If this has un-busied us, send a RR to reopen the window */
  113.         if(len_mbuf(bp) >= axp->window)
  114.                 sendctl(axp,RESPONSE,RR);
  115.         return bp;
  116. }
  117.  
  118. /* Close an AX.25 connection */
  119. void disc_ax25(struct ax25_cb *axp)
  120. {
  121.         switch(axp->state){
  122.         case DISCONNECTED:
  123.                 break;          /* Ignored */
  124.         case SETUP:
  125.                 free_q(&axp->txq);
  126.         case DISCPENDING:
  127.                 lapbstate(axp,DISCONNECTED);
  128.                 del_ax25(axp);
  129.                 break;
  130.         case CONNECTED:
  131.         case RECOVERY:
  132.         case FRAMEREJECT:
  133.                 free_q(&axp->txq);
  134.                 axp->retries = 0;
  135.                 sendctl(axp,COMMAND,DISC|PF);
  136.                 stop_timer(&axp->t3);
  137.                 start_timer(&axp->t1);
  138.                 lapbstate(axp,DISCPENDING);
  139.                 break;
  140.         }
  141. }
  142.  
  143. /* Verify that axp points to a valid ax25 control block */
  144. int ax25val(struct ax25_cb *axp)
  145. {
  146.         register struct ax25_cb *axp1;
  147.         register int i;
  148.  
  149.         if(axp == NULLAX25)
  150.                 return 0;       /* Null pointer can't be valid */
  151.         for(i=0; i < NHASH; i++)
  152.                 for(axp1 = ax25_cb[i];axp1 != NULLAX25; axp1 = axp1->next)
  153.                         if(axp1 == axp)
  154.                                 return 1;
  155.         return 0;
  156. }
  157.  
  158. /* Force a retransmission */
  159. int kick_ax25(struct ax25_cb *axp)
  160. {
  161.         if(!ax25val(axp))
  162.                 return -1;
  163.         recover((int *)axp);
  164.         return 0;
  165. }
  166.  
  167. /* Abruptly terminate an AX.25 connection */
  168. void reset_ax25(struct ax25_cb *axp)
  169. {
  170.         lapbstate(axp,DISCONNECTED);
  171.         del_ax25(axp);
  172. }
  173.